CMAKE_MINIMUM_REQUIRED (VERSION 3.7.2)

IF(NOT CMAKE_BUILD_TYPE)
   SET(CMAKE_BUILD_TYPE "Release")
ENDIF()
	
IF(NOT WIN32)
    SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE})
    SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE})
    SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE})
    SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE})
    SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE})
    SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE})
ENDIF(NOT WIN32)

# Qt library
message(STATUS "Qt6 is used")

IF(DEFINED ENV{QT_DIR})
	set(Qt6Core_DIR "$ENV{QT_DIR}/lib/cmake/Qt6Core")
	set(Qt6_DIR "$ENV{QT_DIR}/lib/cmake/Qt6")
	set(QT_QMAKE_EXECUTABLE "$ENV{QT_DIR}/bin/qmake.exe")
ENDIF()

FIND_PACKAGE(Qt6Core REQUIRED)
FIND_PACKAGE(Qt6Widgets REQUIRED)
FIND_PACKAGE(Qt6OpenGL REQUIRED)
ADD_DEFINITIONS(-DUNICODE)
ADD_DEFINITIONS(-D_UNICODE)
ADD_DEFINITIONS(-DIFCQUERY_STATIC_LIB)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)


SET(viewer_dir ${IFCPP_SOURCE_DIR}/examples/SimpleViewerExampleQt)
SET(RESOURCES ${viewer_dir}/Resources/ifcplusplus.qrc)
QT6_ADD_RESOURCES(SimpleViewerExampleQt_RESOURCES_RCC ${RESOURCES})

FIND_PACKAGE(OpenSceneGraph REQUIRED osgDB osgUtil osgGA osgFX osgSim osgText osgViewer)

SET(IFCPPVIEWER_SOURCE_FILES 
    src/IfcPlusPlusSystem.cpp
    src/main.cpp
    src/cmd/CmdRemoveSelectedObjects.cpp
    src/cmd/Command.cpp
    src/cmd/CommandManager.cpp
	src/gui/EntityAttributeWidget.cpp
    src/gui/IfcTreeWidget.cpp
    src/gui/MainWindow.cpp
    src/gui/OpenFileWidget.cpp
    src/gui/SettingsWidget.cpp
    src/viewer/GraphicsWindowQt.cpp
    src/viewer/IntersectionHandler.cpp
	src/viewer/Orbit3DManipulator.cpp
    src/viewer/ViewerWidget.cpp
)

SET(IFCPPVIEWER_MOC_FILES
    src/IfcPlusPlusSystem.h
	src/gui/EntityAttributeWidget.h
    src/gui/IfcTreeWidget.h
    src/gui/MainWindow.h
    src/gui/OpenFileWidget.h
    src/gui/SettingsWidget.h
    src/viewer/ViewerWidget.h
)

QT6_WRAP_CPP( IFCPPVIEWER_MOC_GENERATED_FILES ${IFCPPVIEWER_MOC_FILES})

LINK_DIRECTORIES(${CMAKE_BINARY_DIR}/IfcPlusPlus/Debug)
LINK_DIRECTORIES(${CMAKE_BINARY_DIR}/IfcPlusPlus/${CMAKE_BUILD_TYPE})

ADD_EXECUTABLE(SimpleViewerExampleQt 
    ${IFCPPVIEWER_MOC_GENERATED_FILES}
    ${IFCPPVIEWER_SOURCE_FILES}
    ${SimpleViewerExampleQt_RESOURCES_RCC}
)

set_target_properties(SimpleViewerExampleQt PROPERTIES DEBUG_POSTFIX "d")
set_target_properties(SimpleViewerExampleQt PROPERTIES CXX_STANDARD 20)
set_target_properties(SimpleViewerExampleQt PROPERTIES VS_DEBUGGER_ENVIRONMENT "PATH=../../IfcPlusPlus/Debug;../../IfcPlusPlus/Release;${OSG_INCLUDE_DIR}/../bin;$(PATH)") # is there a way to set Debug/Release by one variable?

target_link_libraries(SimpleViewerExampleQt
		debug IfcPlusPlusd optimized IfcPlusPlus
        Qt6::Core
        Qt6::Gui
        Qt6::Widgets
        Qt6::OpenGL)
		
If(UNIX)
    # ON Debian Stretch with cmake 3.7.2 OPENTHREADS_LIBRARY_DEBUG is a not known variable
    TARGET_LINK_LIBRARIES(SimpleViewerExampleQt optimized ${OPENTHREADS_LIBRARY})
else(UNIX)
    TARGET_LINK_LIBRARIES(SimpleViewerExampleQt debug ${OPENTHREADS_LIBRARY_DEBUG})
	TARGET_LINK_LIBRARIES(SimpleViewerExampleQt ${OPENTHREADS_LIBRARY} )
endif()

if(USE_OSG_DEBUG)
    target_link_libraries(SimpleViewerExampleQt
        optimized ${OSG_LIBRARY} debug ${OSG_LIBRARY_DEBUG}
        optimized ${OSGDB_LIBRARY} debug ${OSGDB_LIBRARY_DEBUG}
        optimized ${OSGDB_LIBRARY} debug ${OSGDB_LIBRARY_DEBUG}
        optimized ${OSGGA_LIBRARY} debug ${OSGGA_LIBRARY_DEBUG}
        optimized ${OSGUTIL_LIBRARY} debug ${OSGUTIL_LIBRARY_DEBUG}
        optimized ${OSGFX_LIBRARY} debug ${OSGFX_LIBRARY_DEBUG}
        optimized ${OSGVIEWER_LIBRARY} debug ${OSGVIEWER_LIBRARY_DEBUG}
        optimized ${OSGTEXT_LIBRARY} debug ${OSGTEXT_LIBRARY_DEBUG}
    )
else(USE_OSG_DEBUG)
    target_link_libraries(SimpleViewerExampleQt
		${OSG_LIBRARY}
		${OSGDB_LIBRARY}
		${OSGDB_LIBRARY}
        ${OSGGA_LIBRARY}
        ${OSGUTIL_LIBRARY}
        ${OSGFX_LIBRARY}
        ${OSGVIEWER_LIBRARY}
        ${OSGTEXT_LIBRARY}
    )
endif(USE_OSG_DEBUG)

TARGET_INCLUDE_DIRECTORIES(SimpleViewerExampleQt
    PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/src
	${IFCPP_SOURCE_DIR}/IfcPlusPlus/src
	${IFCPP_SOURCE_DIR}/IfcPlusPlus/src/ifcpp/IFC4X3/include
	${IFCPP_SOURCE_DIR}/IfcPlusPlus/src/external
	${IFCPP_SOURCE_DIR}/IfcPlusPlus/src/external/glm
	${IFCPP_SOURCE_DIR}/IfcPlusPlus/src/external/Carve/include
	${IFCPP_SOURCE_DIR}/IfcPlusPlus/src/external/Carve/src
	${IFCPP_SOURCE_DIR}/IfcPlusPlus/src/external/Carve/src/include
	${IFCPP_SOURCE_DIR}/IfcPlusPlus/src/external/Carve/src/common
	${IFCPP_SOURCE_DIR}/IfcPlusPlus/src/external/Carve/build/src
	${IFCPP_SOURCE_DIR}/IfcPlusPlus/src/external/glm
	
    ${OPENTHREADS_INCLUDE_DIR}
    ${OSG_INCLUDE_DIR}
    ${OSGGA_INCLUDE_DIR}
    ${OSGDB_INCLUDE_DIR}
    ${OSGUTIL_INCLUDE_DIR}
    ${OSGANIMATION_INCLUDE_DIR}
    ${OSGTEXT_INCLUDE_DIR}
	${Qt6Core_INCLUDE_DIRS}
)

INSTALL(
    TARGETS SimpleViewerExampleQt
    RUNTIME DESTINATION bin
    LIBRARY DESTINATION bin
    ARCHIVE DESTINATION lib
)
